﻿/***************************************************************************

Copyright (c) Microsoft Corporation. All rights reserved.
This code is licensed under the Visual Studio SDK license terms.
THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

***************************************************************************/

using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Threading;

namespace Microsoft.WowAddonStudio.Project
{
    /// <summary>
    /// This class represent resource storage and management functionality.
    /// </summary>
    internal sealed class Resources
    {
        public const string FormatList = "FormatList";
        public const string GeneralCaption = "GeneralCaption";
        public const string FileListCaption = "FileListCaption";
        public const string AddOnInfoCaption = "AddOnInfoCaption";
        public const string FailedToLoadTemplateFileMessage = "Failed to add template file to project";
        public const string InvalidCustomDeploymentLocationPathMessage = "InvalidWoWIntstallationPathMessage";
        public const string InvalidWoWBenchIntstallationPathMessag = "InvalidWoWBenchIntstallationPathMessag";
        public const string WorldOfWarcraft = "WorldOfWarcraft";

        public const string AddOnStudioforWorldofWarcraft = "AddOn Studio for World of Warcraft";

        private static Resources loader;
        private static object syncObject;

        private readonly ResourceManager resourceManager;

        /// <summary>
        /// public explicitly defined default constructor.
        /// </summary>
        public Resources()
        {
            resourceManager = new ResourceManager("Microsoft.WowAddonStudio.Project.Resources", Assembly.GetExecutingAssembly());
        }

        /// <summary>
        /// Gets the public synchronization object.
        /// </summary>
        private static Object SyncObject
        {
            get
            {
                if (syncObject == null)
                {
                    var o = new Object();
                    Interlocked.CompareExchange(ref syncObject, o, null);
                }

                return syncObject;
            }
        }

        /// <summary>
        /// Gets information about a specific culture.
        /// </summary>
        private static CultureInfo Culture
        {
            get { return CultureInfo.CurrentUICulture; }
        }

        /// <summary>
        /// Gets convenient access to culture-specific resources at runtime.
        /// </summary>
        public static ResourceManager ResourceManager
        {
            get { return GetLoader().resourceManager; }
        }

        /// <summary>
        /// Provide access to the public SR loader object.
        /// </summary>
        /// <returns>Instance of the Resources object.</returns>
        private static Resources GetLoader()
        {
            if (loader == null)
            {
                lock (SyncObject)
                {
                    if (loader == null)
                    {
                        loader = new Resources();
                    }
                }
            }

            return loader;
        }

        /// <summary>
        /// Provide access to resource string value.
        /// </summary>
        /// <param name="name">Received string name.</param>
        /// <param name="args">Arguments for the String.Format method.</param>
        /// <returns>Returns resources string value or null if error occured.</returns>
        public static string GetString(string name, params object[] args)
        {
            Resources resourcesInstance = GetLoader();

            if (resourcesInstance == null)
                return null;

            string res = resourcesInstance.resourceManager.GetString(name, Culture);

            if (args != null && args.Length > 0)
                return String.Format(CultureInfo.CurrentCulture, res, args);

            return res;
        }

        /// <summary>
        /// Provide access to resource string value.
        /// </summary>
        /// <param name="name">Received string name.</param>
        /// <returns>Returns resources string value or null if error occured.</returns>
        public static string GetString(string name)
        {
            Resources resourcesInstance = GetLoader();

            if (resourcesInstance == null)
                return null;

            return resourcesInstance.resourceManager.GetString(name, Culture);
        }

        /// <summary>
        /// Provide access to resource object value.
        /// </summary>
        /// <param name="name">Received object name.</param>
        /// <returns>Returns resources object value or null if error occured.</returns>
        public static object GetObject(string name)
        {
            Resources resourcesInstance = GetLoader();

            if (resourcesInstance == null)
                return null;

            return resourcesInstance.resourceManager.GetObject(name, Culture);
        }
    }
}